package com.css.dzj.dzjzhfx.modules.behavior;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjzhfx.common.audit.service.SysAuditService;
import com.css.dzj.dzjzhfx.common.util.HttpClientUtil;
import com.css.dzj.dzjzhfx.common.util.R;
import com.css.dzj.dzjzhfx.common.util.Constant;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.*;

@RestController
@PropertySource(value = "classpath:config.properties")
@RequestMapping("/behavior")
public class Behavior {
    @Value("${apURL}")
    private String apURL;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    private HttpServletRequest request;

    /***
     * 获取检查行为
     * @return
     */
    @GetMapping("/getcount")
    public R count() {
        String s = "";
        try {
            /***
             * 获取检查行为
             */
            String msg = HttpClientUtil.doGet(apURL + "/check/count");
            JSONObject jsonObject = JSON.parseObject(msg);
            Object count = jsonObject.get("count");
            Map<String, Object> mapCheck = JSONObject.parseObject(count.toString());
            /**
             * 获取处罚行为
             */
            String msgPunish = HttpClientUtil.doGet(apURL + "/punish/punish");
            JSONObject jsonPunish = JSON.parseObject(msgPunish);
            Object punishOb = jsonPunish.get("count");
            Map<String, Object> mapPunish = JSONObject.parseObject(punishOb.toString());

            /**
             * 获取强制行为
             */
            String msgForce = HttpClientUtil.doGet(apURL + "/force/force");
            JSONObject jsonForce = JSON.parseObject(msgForce);
            Object forceOb = jsonForce.get("count");
            Map<String, Object> mapForce = JSONObject.parseObject(forceOb.toString());

            /**
             * 获取其他行为
             */
            String msgOther = HttpClientUtil.doGet(apURL + "/other/other");
            JSONObject jsonOther = JSON.parseObject(msgOther);
            Object otherOb = jsonOther.get("count");
            Map<String, Object> mapOther = JSONObject.parseObject(otherOb.toString());

            List<Map<String, Object>> list = new ArrayList();
            list.add(mapCheck);
            list.add(mapPunish);
            list.add(mapForce);
            list.add(mapOther);
            Map<String, Object> mapAll = new HashMap<>();
            for (Map<String, Object> map : list) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    String key = entry.getKey();
                    Integer value = (Integer) entry.getValue();
                    Integer valueAll = (Integer) mapAll.get(entry.getKey());
                    if (valueAll == null) {
                        mapAll.put(key, value);
                    } else {
                        valueAll = valueAll.intValue() + value.intValue();
                        mapAll.put(key, valueAll);
                    }
                }
            }
            List<Map<String, Object>> listNew = new ArrayList();
            Integer num = 0;
            for (Map.Entry<String, Object> entry : mapAll.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                num += value;
                map.put("name", name);
                map.put("value", value);
                listNew.add(map);
            }
            String data = JSON.toJSONString(listNew);
            sysAuditService.saveSuccessAudit("查询成功", Constant.BEHAVIOR, Constant.PT, Constant.ZHFX_SELECT, data, request);
            return R.ok().put("data", data).put("cityTotal", num);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.BEHAVIOR, Constant.YZ, Constant.ZHFX_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 获取饼状图行为
     * @return
     */
    @GetMapping("/get")
    public R getPunish() {
        String s = "";
        try {
            /***
             * 获取检查行为
             */
            String msgCheck = HttpClientUtil.doGet(apURL + "/check/count");
            JSONObject jsonObject = JSON.parseObject(msgCheck);
            Object count = jsonObject.get("count");
            Map<String, Object> mapCheck = JSONObject.parseObject(count.toString());
            List listCheck = new ArrayList();
            List<String> check = new ArrayList<>();
            for (Map.Entry<String, Object> entry : mapCheck.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listCheck.add(map);
                check.add(name);
            }
            String dataCheck = JSON.toJSONString(listCheck);
            /**
             * 获取处罚行为
             */
            String msgPunish = HttpClientUtil.doGet(apURL + "/punish/punish");
            JSONObject jsonPunish = JSON.parseObject(msgPunish);
            Object punishOb = jsonPunish.get("count");
            Map<String, Object> mapPunish = JSONObject.parseObject(punishOb.toString());
            List listPunish = new ArrayList();
            List<String> punish = new ArrayList<>();
            for (Map.Entry<String, Object> entry : mapPunish.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listPunish.add(map);
                punish.add(name);
            }
            String dataPunish = JSON.toJSONString(listPunish);
            /**
             * 获取强制行为
             */
            String msgForce = HttpClientUtil.doGet(apURL + "/force/force");
            JSONObject jsonForce = JSON.parseObject(msgForce);
            Object forceOb = jsonForce.get("count");
            Map<String, Object> mapForce = JSONObject.parseObject(forceOb.toString());
            List listForce = new ArrayList();
            List<String> force = new ArrayList<>();
            for (Map.Entry<String, Object> entry : mapForce.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listForce.add(map);
                force.add(name);
            }
            String dataForce = JSON.toJSONString(listForce);
            /**
             * 获取其他行为
             */
            String msgOther = HttpClientUtil.doGet(apURL + "/other/other");
            JSONObject jsonOther = JSON.parseObject(msgOther);
            Object otherOb = jsonOther.get("count");
            Map<String, Object> mapOther = JSONObject.parseObject(otherOb.toString());
            List listOther = new ArrayList();
            List<String> other = new ArrayList<>();
            for (Map.Entry<String, Object> entry : mapOther.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listOther.add(map);
                other.add(name);
            }
            String dataOther = JSON.toJSONString(listOther);
            List list = new ArrayList();
            for (String chk : check) {
                list.add(chk);
            }
            for (String pun : punish) {
                if (!list.contains(pun)) {
                    list.add(pun);
                }
                ;
            }
            for (String forc : force) {
                if (!list.contains(forc)) {
                    list.add(force);
                }
            }

            for (String oth : other) {
                if (!list.contains(oth)) {
                    list.add(oth);
                }
            }
            //TODO
            s = JSON.toJSONString(list);
            sysAuditService.saveSuccessAudit("查询成功", Constant.BEHAVIOR, Constant.PT, Constant.ZHFX_SELECT, s, request);
            return R.ok().put("dataCheck", dataCheck).put("dataPunish", dataPunish).
                    put("dataForce", dataForce).put("dataOther", dataOther).put("name", list);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.BEHAVIOR, Constant.YZ, Constant.ZHFX_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 柱状图
     * @return
     */
    @GetMapping("/getbar")
    public R getBar() {
        String s = "";
        try {
            /***
             * 获取检查行为
             */
            String msgCheck = HttpClientUtil.doGet(apURL + "/check/count");
            JSONObject jsonObject = JSON.parseObject(msgCheck);
            Object count = jsonObject.get("count");
            Map<String, Object> mapCheck = JSONObject.parseObject(count.toString());
            List listCheck = new ArrayList();
            List<String> check = new ArrayList<>();
            /**
             * 获取处罚行为
             */
            String msgPunish = HttpClientUtil.doGet(apURL + "/punish/punish");
            JSONObject jsonPunish = JSON.parseObject(msgPunish);
            Object punishOb = jsonPunish.get("count");
            Map<String, Object> mapPunish = JSONObject.parseObject(punishOb.toString());
            List<String> punish = new ArrayList<>();
            List listPunish = new ArrayList();
            /**
             * 获取强制行为
             */
            String msgForce = HttpClientUtil.doGet(apURL + "/force/force");
            JSONObject jsonForce = JSON.parseObject(msgForce);
            Object forceOb = jsonForce.get("count");
            Map<String, Object> mapForce = JSONObject.parseObject(forceOb.toString());
            List listForce = new ArrayList();
            List<String> force = new ArrayList<>();

            /**
             * 获取其他行为
             */
            String msgOther = HttpClientUtil.doGet(apURL + "/other/other");
            JSONObject jsonOther = JSON.parseObject(msgOther);
            Object otherOb = jsonOther.get("count");
            Map<String, Object> mapOther = JSONObject.parseObject(otherOb.toString());
            List listOther = new ArrayList();
            List<String> other = new ArrayList();
            /***
             * 获取检查行为的机构
             */
            for (Map.Entry<String, Object> entry : mapCheck.entrySet()) {
                String name = entry.getKey();
                check.add(name);
            }
            /***
             * 获取处罚行为的机构
             */
            for (Map.Entry<String, Object> entry : mapPunish.entrySet()) {
                String name = entry.getKey();
                punish.add(name);
            }
            /**
             * 获取强制行为的机构
             */
            for (Map.Entry<String, Object> entry : mapForce.entrySet()) {
                String name = entry.getKey();
                force.add(name);
            }
            /***
             * 获取其他行为的机构
             */
            for (Map.Entry<String, Object> entry : mapOther.entrySet()) {
                String name = entry.getKey();
                other.add(name);
            }
            for (String chk : check) {
                if (!punish.contains(chk)) {
                    mapPunish.put(chk, 0);
                }
                if (!force.contains(chk)) {
                    mapForce.put(chk, 0);
                }
                if (!other.contains(chk)) {
                    mapOther.put(chk, 0);
                }
            }
            for (String pun : punish) {
                if (!check.contains(pun)) {
                    mapCheck.put(pun, 0);
                }
                if (!force.contains(pun)) {
                    mapForce.put(pun, 0);
                }
                if (!other.contains(pun)) {
                    mapOther.put(pun, 0);
                }
            }
            for (String fore : force) {
                if (!check.contains(fore)) {
                    mapCheck.put(fore, 0);
                }
                if (!punish.contains(fore)) {
                    mapPunish.put(fore, 0);
                }
                if (!other.contains(fore)) {
                    mapOther.put(fore, 0);
                }
            }
            for (String oth : other) {
                if (!check.contains(oth)) {
                    mapCheck.put(oth, 0);
                }
                if (!force.contains(oth)) {
                    mapForce.put(oth, 0);
                }
                if (!punish.contains(oth)) {
                    mapPunish.put(oth, 0);
                }
            }
            for (Map.Entry<String, Object> entry : mapCheck.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listCheck.add(map);
            }
            String dataCheck = JSON.toJSONString(listCheck);

            for (Map.Entry<String, Object> entry : mapPunish.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listPunish.add(map);
            }
            String dataPunish = JSON.toJSONString(listPunish);

            for (Map.Entry<String, Object> entry : mapForce.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listForce.add(map);
            }
            String dataForce = JSON.toJSONString(listForce);

            for (Map.Entry<String, Object> entry : mapOther.entrySet()) {
                HashMap map = new HashMap();
                String name = entry.getKey();
                Integer value = (Integer) entry.getValue();
                map.put("name", name);
                map.put("value", value);
                listOther.add(map);
            }
            String dataOther = JSON.toJSONString(listOther);
        /*List list=new ArrayList();
        for(String chk:check){
            list.add(chk);
        }
        for(String pun:punish){
            if(!list.contains(pun)){
                list.add(pun);
            };
        }
        for (String forc:force){
            if(!list.contains(forc)){
                list.add(force);
            }
        }

        for(String oth:other){
            if(!list.contains(oth)){
                list.add(oth);
            }
        }*/
            //TODO
            s = JSON.toJSONString(dataCheck);
            sysAuditService.saveSuccessAudit("查询成功", Constant.BEHAVIOR, Constant.PT, Constant.ZHFX_SELECT, s, request);
            return R.ok().put("dataCheck", dataCheck).put("dataPunish", dataPunish).
                    put("dataForce", dataForce).put("dataOther", dataOther);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.BEHAVIOR, Constant.YZ, Constant.ZHFX_SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }
}
